﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LZH.Models;
using NFine.Code;
using LZH.BLL;

namespace WebAPI.Controllers
{


    [RoutePrefix(prefix:"api/Users")]
    [MyAuth] 
    public class UsersTableController : ApiController
    {
        UserTableBLL BLL = new UserTableBLL();

        [Route(template:"Add")]
        public IHttpActionResult Get()
        {
            return Ok(new { data = 2 });
        }


        [AllowAnonymousAttribute]
        [Route(template: "CheckUser"),HttpPost]
        public IHttpActionResult Login(LoginViewModel model)
        {
            UserTable UserModel = BLL.IQueryable(x => x.accountNumber == model.accountNumber && x.passWord == model.passWord).FirstOrDefault<UserTable>();
            if (UserModel != null)
            {
                //加密串
                var to_ken = JwtTools.Encoder(new Dictionary<string, object>()
                         {
                             { "userNames",UserModel.userNames},
                             { "Id",UserModel.Id}
                         });
                var data = new {
                   token= to_ken,
                   userName= UserModel.userNames
                };
                return this.SendData("登录成功", data);

            }
            else {
                return this.ErrorData("账号或密码错误。");
            }

        }

        [Route(template:"GetList"),HttpGet]
        public IHttpActionResult GetList(string userNames, string role, int pageSize,  int  pageIndex)
        {
            var expression = ExtLinq.True<UserTable>();
            if (!string.IsNullOrEmpty(userNames))
            {
                expression = expression.And(t => t.userNames.Contains(userNames));
            }
            if (role!= "全部用户")
            {
                expression = expression.And(t => t.role.Contains(role));
            }
            expression = expression.And(t => t.IsDelete == false);
            int totalRecord = 10;
            return Ok(new {
                list = BLL.LoadPageItems(pageSize, pageIndex, out totalRecord, expression, u => u.CreateTime, false),
                pageTotal = totalRecord
            });
        }
        [Route(template: "DeleteOrUpdate"), HttpPost]
        public IHttpActionResult UpdateUser(UserTable model)
        {
            if (string.IsNullOrEmpty(model.Id))
            {
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = System.DateTime.Now;
                BLL.Insert(model);
                return this.SendData("添加", model);
            }
            else 
            {
                BLL.Update(model);
                return this.SendData("修改");
            }
        }
        [Route(template: "DeleteUser"), HttpPost]
        public IHttpActionResult DeleteUser(UserTable model)
        {  
            BLL.Delete(model);
            return this.SendData("删除成功");
        }

        [Route(template: "BatchDelete"), HttpPost]
        public IHttpActionResult BatchDelete([FromBody] List<UserTable> models)
        {
               BLL.Delete(models);
            return this.SendData("删除成功");

        }






    }
}

